package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.PaymentMethodExcution;
import com.fortune.dto.in.paymentMethod.PaymentMethodAddDto;
import com.fortune.dto.in.paymentMethod.PaymentMethodEditDto;
import com.fortune.dto.in.paymentMethod.PaymentMethodListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.PaymentMethodEnum;
import com.fortune.service.PaymentMethodService;
import com.fortune.vo.paymentMethod.PaymentMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Api(value = "product", description = "付款方式管理", tags = {"付款方式管理相关"})
@RestController
@RequestMapping("paymentMethod")
public class PaymentMethodController {
    @Autowired
    private PaymentMethodService paymentMethodService;

    @ApiOperation(value = "付款方式列表", nickname = "paymentMethodList", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int")
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<PaymentMethod>> list(HttpServletRequest request, @ApiIgnore @Validated PaymentMethodListDto paymentMethodListDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        paymentMethodListDto.setPage((paymentMethodListDto.getPage()-1)*10);
        List<PaymentMethod> list = paymentMethodService.queryPaymentMethod(companyId,paymentMethodListDto);
        int count = paymentMethodService.queryPaymentMethodCount(companyId,paymentMethodListDto);
        PagingData<PaymentMethod> pagingData = new PagingData<>();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new PaymentMethodExcution<PagingData<PaymentMethod>>(pagingData, PaymentMethodEnum.SUCCESS);
    }

    @ApiOperation(value = "添加付款方式", nickname = "paymentMethodAdd", notes = "" ,tags = {"@石匡"})
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody PaymentMethodAddDto paymentMethodAddDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num = paymentMethodService.queryAddPaymentMethod(companyId,paymentMethodAddDto);
        if(num > 0){
            return new PaymentMethodExcution<Void>(null, PaymentMethodEnum.EXIST);
        }
        num = paymentMethodService.addPaymentMethod(companyId,paymentMethodAddDto);
        if(num < 1){
            return new PaymentMethodExcution<Void>(null, PaymentMethodEnum.FAILURE);
        }
        return new PaymentMethodExcution<Void>(null, PaymentMethodEnum.SUCCESS);
    }

    @ApiOperation(value = "编辑付款方式", nickname = "paymentMethodEdit", notes = "" ,tags = {"@石匡"})
    @PutMapping(value = "edit",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody PaymentMethodEditDto paymentMethodEditDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num = paymentMethodService.queryEditPaymentMethod(companyId,paymentMethodEditDto);
        if(num > 0){
            return new PaymentMethodExcution<Void>(null, PaymentMethodEnum.EXIST);
        }
        num = paymentMethodService.editPaymentMethod(companyId,paymentMethodEditDto);
        if(num < 1){
            return new PaymentMethodExcution<Void>(null, PaymentMethodEnum.FAILURE);
        }
        return new PaymentMethodExcution<Void>(null, PaymentMethodEnum.SUCCESS);
    }

    @ApiOperation(value = "删除付款方式", nickname = "paymentMethodDelete", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParam(name="paymentMethodId", value="付款方式id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotEmpty(message="商品单位id不能为空") String paymentMethodId) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num = paymentMethodService.deletePaymentMethod(companyId,paymentMethodId);
        if(num < 1){
            return new PaymentMethodExcution<Void>(null, PaymentMethodEnum.FAILURE);
        }
        return new PaymentMethodExcution<Void>(null, PaymentMethodEnum.SUCCESS);
    }
}
